package com.lt.flashcard.view;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.xel.fn.StringFns;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.Cart;
import com.lt.flashcard.model.entity.FlashcardSet;
import com.lt.flashcard.model.entity.Productcategory;
import com.lt.flashcard.model.entity.Promotion;
import com.lt.flashcard.model.service.EcommerceService;
import com.lt.flashcard.model.service.FlashcardSetService;
import com.lt.flashcard.model.service.Repository;

/**
 * Display detail of product
 * @author ocsen
 *
 */
public class StoreDetailVm {

    private FlashcardSet fcs; // Current flashcard set
    private List<Productcategory> categories = null;
    private Long category = null;
    private Set<Long> purchased = new HashSet<Long>();

    @Init
    public void init(@ContextParam(ContextType.PAGE) Page page) {
        String id = Executions.getCurrent().getParameter("id");
        if (StringUtils.isBlank(id)) {
            Utility.displayAccessDenied(false);
            return;
        }

        Long lid = Long.parseLong(id);
        FlashcardSetService fsrv = Repository.getInstance().getFlashcardSetService();
        fcs = fsrv.findByID(lid);

        if (fcs == null) {
            Utility.displayAccessDenied(false);
            return;
        }
        // redirect in case this is normal product
        if (fcs.getAdmin() == FlashcardSet.ADMIN_NO) {
            String url = Utility.generateUrl("flashcardset", fcs.getId(), fcs.getName());
            Utility.redirect(Utility.removeContextPath(url), false);
            return;
        }

        if (fcs.getProductcategory() != null) {
            category = fcs.getProductcategory().getId();
        }

        Utility.addOgTag(page, Utility.generateTitle("flashcardset", fcs), Utility.generateDescription("flashcardset", fcs),
                Utility.loadImageFromGridFS(fcs.getImage(), Utility.DEF_FLASHCARDSET));

        EcommerceService srv = Repository.getInstance().getEcommerceService();
        setPurchased(srv.getPurchasedProductsId(Utility.getUser()));
    }

    /**
     * Get the card
     * @return
     */
    public Cart getCart() {
        EcommerceService srv = Repository.getInstance().getEcommerceService();
        return srv.getCurrentCart();
    }

    public FlashcardSet getFcs() {
        return fcs;
    }

    /**
     * Return promotion string
     * @return
     */
    public String getPromotionStr() {
        List<String> rs = new ArrayList<String>();
        EcommerceService srv = Repository.getInstance().getEcommerceService();
        List<Promotion> pros = srv.getAllValidPromotions(Promotion.TARGETTYPE_FLASHCARDSET, Promotion.CONDITIONTYPE_BUYITEM, fcs.getId());

        for (Promotion p : pros) {
            switch (p.getType()) {
            case Promotion.TYPE_ADDBLANKCARD:
                rs.add(String.format("%s flashcard trắng", p.getValue()));
                break;
            case Promotion.TYPE_ADDYELLOWCOIN:
                rs.add(String.format("<span>%s</span><img src=images/gold_pd.png' />", p.getValue()));
                break;
            case Promotion.TYPE_ADDBLUECOIN:
                rs.add(String.format("<span>%s</span><img src=images/gold_pd.png' />", p.getValue()));
                break;
            case Promotion.TYPE_GAINMEDAL:
                rs.add("Gain medal");
                break;
            }
        }
        return rs.size() > 0 ? StringFns.join(rs.toArray(), " và ") : "";
    }

    @NotifyChange("cart")
    @Command
    public void addtocard() {
        Cart cart = getCart();
        if (cart != null) {
            cart.addItem(fcs.getId());
        }
    }

    public List<Productcategory> getCategories() {
        if (categories == null) {
            EcommerceService srv = Repository.getInstance().getEcommerceService();
            categories = srv.getParentProductCategories();
        }
        return categories;
    }

    public void setCategories(List<Productcategory> categories) {
        this.categories = categories;
    }

    public Long getCategory() {
        return category;
    }

    public void setCategory(Long category) {
        this.category = category;
    }

    /**
     * @return the purchased
     */
    public Set<Long> getPurchased() {
        return purchased;
    }

    /**
     * @param purchased the purchased to set
     */
    public void setPurchased(Set<Long> purchased) {
        this.purchased = purchased;
    }
}
